<?php
$this->load->view('layout/header', ["title"=> "接口调试首页"]);
$this->load->view('layout/topnav');
?>
<div style="margin:0px auto; margin-top:10px; width:1000px;">
  <ul>
    <li>接口名称：<?php echo $api["name"]?></li>
    <li>接口地址：<?php echo $host.$api["uri"]?></li>
    <li>接口方法：<?php echo $api["method"]?></li>
  </ul>
</div>

<br>
<table align="center" width="1000" cellpadding="0" cellspacing="1" style="border:1px solid #CCC">
  <form id="requestform">
  <tr bgcolor="#EEE" height="24">
    <td width="120">参数名</td>
    <td width="400">参数</td>
    <td>必填</td>
    <td>类型</td>
    <td>说明</td>
  </tr>
<?php

if(isset($api["params"]) && count($api["params"])>0){

	foreach($api["params"] as $param){
		$value = is_array($param["default"]) ? json_encode($param["default"]) : $param["default"];
?>
  <tr bgcolor="#F9F9F9" height="24">
    <td><?php echo $param["name"]?></td>
<?php
		if(strtoupper($param["type"]) == "ENUM"){
?>
    <td><select id=""></select></td>
<?php
		}else if(strtoupper($param["type"]) == "JSON"){
?>
    <td><textarea name="<?php echo $param["name"]?>" cols="50" rows="8"><?php echo $value;?></textarea></td>
<?php
		}else{
?>
	<td><input name="<?php echo $param["name"]?>" type="text" value='<?php echo $value?>'/></td>
<?php
		}
?>
    <td><?php echo $param["necessary"] ? '是' : '否';?></td>
    <td><?php echo $param["type"]?></td>
    <td><?php echo $param["intro"]?></td>
  </tr>
<?php
	}
}
?>

  <tr bgcolor="#F9F9F9" height="24">
    <td colspan="5" align="center">
<?php
	if(strtoupper($api["method"]) == 'POST'){
?>
		<input type="button" value="CURL请求" onclick="request_curl()"/>
		<input type="button" value="CURL RAW" onclick="request_curl_raw()"/>
		<input type="button" value="直接请求" onclick="request()"/></td>
<?php
	}else{
	?>
		<input type="button" value="CURL请求" onclick="request_curl()"/>
		<input type="button" value="直接请求" onclick="request()"/>
	<?php
	}
?>
      </td>
  </tr>
  </form>
</table>
<script language="javascript">

var host = "<?php echo $host?>";
var api = <?php echo json_encode($api, JSON_UNESCAPED_UNICODE)?>;
var isAuth = <?php echo $api["isAuth"] ? 'true' : 'false'?>;
var uri = "<?php echo $api["uri"]?>";
var method = "<?php echo $api["method"]?>";
var params = <?php echo (isset($api["params"]) && count($api["params"])>0) ? json_encode($api["params"], JSON_UNESCAPED_UNICODE) : "[]"?>;
var access_token = "<?php echo (isset($this->access_token) && !empty($this->access_token)) ? $this->access_token : "";?>";

console.log(api);
console.log(params);
	
function request_curl(){
	var $form = $('#requestform');
	$form.attr('method', 'POST');
	$form.append($('<input type="text" name="uri" value="'+uri+'"/>'));
	$form.attr('action', "/search/asyncDetail.html?uri="+uri);
	$form.submit();
}

function request_curl_raw(){
	var $form = $('#requestform');
	$form.attr('method', 'POST');
	$form.append($('<input type="text" name="uri" value="'+uri+'"/>'));
	$form.attr('action', "/api/raw.html?uri="+uri);
	$form.submit();
}

/**
 * 直接请求
 */
function request(){

	var request_url = host + api.uri;
	console.log(request_url);

	var $form = $('#requestform');
	
	console.log(api);
	
	if(api.method == "GET"){

		$form.attr('method', 'GET');
		$form.attr('target', '_blank');
		$form.attr('action', request_url);
		
		if(access_token != ""){
			$form.append($('<input type="text" name="access_token" value="'+access_token+'"/>'));  
		}
		
		$form.submit();
	}else if(api.method == "POST"){
		
		$form.attr('method', 'POST');
		$form.attr('target', '_blank');
		$form.attr('action', request_url);
		
		if(access_token != ""){
			$form.append($('<input type="text" name="access_token" value="'+access_token+'"/>'));  
		}
		
		$form.submit();
	}
	

}


</script>
</body>
</html>